//定义一个接口

//1.接口对json的约束

interface Info{
    firstName:string,
    lastName:string,
    age?:number, //可选属性
    [propsName:string]:any //任意多个属性  键数据类型是string，值是any
}

// function getInfo(firstName:string,lastName:string,age:number):string{

// }

function getInfo(info:Info):string{
    let {firstName,lastName,age} = info;
    if(age){
        return `${firstName}${lastName}${age}`
    }
    return `${firstName}${lastName}`
}

console.log(getInfo({firstName:'zs',lastName:'zhang',age:19,address:'北京',idCard:'1221112'}));

//2.接口对函数的约束

interface CompareInterface{
    (num1:number,num2:number):boolean;
}

// function compareFun(num1:number,num2:number):boolean{
//     return num1>num2
// }

// let compareFun:CompareInterface = function(num1:number,num2:number):boolean{
//     return num1>num2
// }

let compareFun:CompareInterface = function(num1,num2){
    return num1>num2
}
console.log(compareFun(11,2))

//接口对类的约束

interface Animal{
    name:string,
    eat(str:string):void
}

class Dog implements Animal{
    name:string;
    constructor(name:string){
        this.name = name;
    }
    eat(str:string):void{
        console.log(this.name+'吃'+str);
    }
}
let d1 = new Dog('藏獒');
d1.eat('肉')

class Cat implements Animal{
    name:string;
    constructor(name:string){
        this.name = name;
    }
    eat(str:string):void{
        console.log(this.name+'吃'+str);
    }
}
let c1 = new Dog('无毛猫');
c1.eat('鱼')

